package com.navinfo.tripanalysis.util;


import com.navinfo.tripanalysis.common.util.ArithmeticUtils;
import com.navinfo.tripanalysis.common.arithmetic.common.CommonData;
import com.navinfo.tripanalysis.common.arithmetic.common.DrivingData;
import com.navinfo.tripanalysis.pojo.Point0200;
import com.navinfo.tripanalysis.pojo.Point;
import com.navinfo.tripanalysis.pojo.Point0F37;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;


/**
 */
public class IntegrateSupportUtil {
    private static final Logger logger = LoggerFactory.getLogger(IntegrateSupportUtil.class);

    /**
     * 中间变量的公共参数CommonData赋值
     * @param point
     * @param drivingData
     * @return
     */
    public static DrivingData assign(Point point, DrivingData drivingData) {
        CommonData commonData = drivingData.getCommonData();
        commonData.setGpsTime(point.getGpsTime());
        commonData.setLongitude(point.getLongitude());
        commonData.setLatitude(point.getLatitude());
        commonData.setHeight((int) point.getHeight());
        commonData.setGpsDirection(point.getDirection());
        commonData.setSpeedKmh((int) point.getSpeedKmh());
        commonData.setSpeedMs(BigDecimal.valueOf(point.getSpeedKmh()).divide(new BigDecimal("3.6"),  2, BigDecimal.ROUND_HALF_UP));
        commonData.setRotation(point.getRotation() );
        commonData.setAccelerator(point.getAccPedalPos() );
        commonData.setClutchSwitch(point.getClutchSwitch() );
        commonData.setEngineOutputTorque(point.getEngineOutputTorque() );
        commonData.setRealTimeOilConsumption(BigDecimal.valueOf(point.getRealTimeOilConsumption() ));
        commonData.setFuelConsumptionRate(BigDecimal.valueOf(point.getFuelConsumptionRate() ));

        switch (point.getProtocol()){
            case Point.PROTOCOL_0200:
                assign0200Data(drivingData,(Point0200)point);
                break;
            case Point.PROTOCOL_0F37:
                Point0F37 point0F37 = (Point0F37) point;
                commonData.setBrake(point0F37.getBrake());
                commonData.setAtmosphericTem(point0F37.getAtmosphericTem());
                commonData.setInstrumentSpeed((int)point0F37.getInstrumentSpeed());
                commonData.setUreaInjection(point0F37.getUreaInjection());
                commonData.setUreaLevel(point0F37.getUreaLevel());
                break;
            default:
                break;
        }

        //需要上个点属性计算的指标
        if (commonData.getLastGpsTime() > 0) {
            //与上个点的时间差（毫秒）
            long timeInterval = point.getGpsTime() - commonData.getLastGpsTime();
            commonData.setDffTime((int) timeInterval);

            //计算加速度
            try {
                commonData.setAcceleratedSpeed( ArithmeticUtils.calculateAcceleratedSpeed(commonData.getLastSpeedMs().doubleValue(), commonData.getSpeedMs().doubleValue(), timeInterval));
            }catch (Exception ex){
                //如果计算加速度报错，则使用上一次的加速度
                logger.error(ex.getMessage(),ex);
            }
        }

        return drivingData;
    }

    /**
     * 将0200相关指标放到drivingData的commonData中
     * @param drivingData
     * @param pojo0200
     */
    public static void assign0200Data(DrivingData drivingData, Point0200 pojo0200) {
        CommonData data = drivingData.getCommonData();
        data.setAccStatus(pojo0200.getAccStatus() == 1);
        data.setTotalOil(pojo0200.getTotalFuelCons());
        data.setDpfPressure(pojo0200.getIntakePressure());
        data.setWaterTemp(1.0*pojo0200.getCoolingWaterTem());
        data.setIntakeAirTem(pojo0200.getIntakeAirTem());
        data.setCruiseControl(pojo0200.isCruiseControl());
        data.setOilPercentage(pojo0200.getOilPercentage());
        data.setHandBrakeStatus(pojo0200.getHandBrakeStatus());
        data.setSlideNeutralStatus(pojo0200.getSlideNeutralStatus());
        data.setRapidAccelerationStatus(pojo0200.getRapidAccelerationStatus());
        data.setRapidDecelerationStatus(pojo0200.getRapidDecelerationStatus());
        data.setSharpTurningStatus(pojo0200.getSharpTurningStatus());
        data.setSlamTheAcceleratorStatus(pojo0200.getSlamAcceleratorStatus());
        data.setAmtLoad(pojo0200.getAmtLoad());
        data.setVecuLoad(pojo0200.getVecuLoad());
    }

}